package cn.his.medical.phismange.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.his.medical.phismange.R;
import cn.his.medical.phismange.bean.Demo;
import cn.his.medical.phismange.utils.CommonTools;

/**
 * @类名 IndicatorView
 * @描述
 * @作者 xiewei
 * @创建时间 2018/4/24 13:29
 */
public class IndicatorView extends View {
    private Paint mPaint;
    private int radiusNumber = 0;//画的圆的数量
    private int raduis = 30;//圆的半径
    float Float[]; //虚线
    private List<Demo> list = new ArrayList();//显示参数的属性

    public IndicatorView(Context context) {
        this(context, null);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化画笔
        mPaint = new Paint();
        this.mPaint.setAntiAlias(true); //消除锯齿
        this.mPaint.setStyle(Paint.Style.FILL); //绘制空心圆
    }
    int radius = 30;//圆的半径
    int vivio = 4;//控制显示多少个实心圆
    float mFloat[];
    @Override
    protected void onDraw(Canvas canvas) {
        //平分控件的高度,每段的高度
        radius = CommonTools.dip2px(getContext(), 15);
        int height = getHeight() / 2;
        //平分宽度十等分
        int width = getWidth() / (5 * 2);
        //画虚线的设置////////start///////////
        int cc = 2 * width - 2 * radius - 4;//计算虚线或者实线的宽度 ，-4是因为左右两边圆各自留出2的距离 查看99和100行
        mFloat = new float[cc / 2];
        for (int i = 0; i < cc / 2; i++) {
            mFloat[i] = 3;//虚线的宽度初始化
        }

        //画虚线的大小////////end///////////

        //1 画最上层的圆///////////////start//////////////
        int circleY = height - radius - radius / 2;
        for (int i = 0; i < mornal.size(); i++) {
            Demo info = mornal.get(i);
            int circleX = width + i * 2 * width;
            this.mPaint.setARGB(255, 153, 153, 153);
            this.mPaint.setStrokeWidth(2);
            if (info.getStatus().equals("0")) { //画虚圆
                canvas.drawCircle(circleX, circleY, radius, this.mPaint);
            } else { //画实圆
                drawReal(circleX, circleY, canvas);
            }
        }
        boolean really = false;
        for (int j = 0; j < mornal.size() - 1; j++) { //画线
            really = true;
            int n = j + 1;
            if (n < mornal.size()) {
                Demo info1 = mornal.get(n);
                if (info1.getStatus().equals("1"))//获取下一个数据是空的
                    really = false;
            }
            int lineX = width + j * 2 * width + radius + 2; //+2是为了留出缝隙
            int lineX1 = (width + j * 2 * width) + 2 * width - radius - 2;//-2是为了留出缝隙

            if (!really) { //画虚线
                Paint mPaint = new Paint();
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setColor(Color.DKGRAY);
                Path path = new Path();
                path.moveTo(lineX, circleY);//起点 x/y
                path.lineTo(lineX1, circleY);//终点 x/y
                PathEffect effects = new DashPathEffect(mFloat, 1);//虚线数组
                mPaint.setPathEffect(effects);
                canvas.drawPath(path, mPaint);

            } else {//画实线

                this.mPaint.setARGB(255, 255, 108, 0);
                canvas.drawLine(lineX, circleY, lineX1, circleY, mPaint);
            }


        }


        //1 画最上层的圆///////////////end//////////////

        //2 画最下层的文字///////////////start//////////////


        int txtY = height + radius;//第一行文字的Y高度

        Paint mmPaintLines = new Paint(mPaint.ANTI_ALIAS_FLAG);
        mmPaintLines.setStrokeWidth(3);
        mmPaintLines.setTextSize(CommonTools.dip2px(getContext(), 14));
        mmPaintLines.setTextAlign(Paint.Align.LEFT);

        Rect rect = new Rect();

        for (int i = 0; i < mornal.size(); i++) {

            Demo info = mornal.get(i);

            int lineX = width + i * 2 * width;//每个字符串x坐标

            mmPaintLines.getTextBounds(info.text, 0, 1, rect);//可以获取字体宽高

            float awayY2 = txtY + rect.height() + radius / 2;//底层文字y高度计算


            if (info.getStatus().equals("1")) {//需要显示的时间和文字

                //画时间
                mmPaintLines.setColor(getResources().getColor(R.color.colorAccent));

                mmPaintLines.getTextBounds(info.time, 0, info.time.length(), rect);
                canvas.drawText(info.time, lineX - rect.width() / 2, txtY, mmPaintLines);

                //画送货的状态
                mmPaintLines.setColor(getResources().getColor(R.color.colorPrimary));
                mmPaintLines.getTextBounds(info.text, 0, info.text.length(), rect);
                canvas.drawText(info.text, lineX - rect.width() / 2, awayY2, mmPaintLines);

            } else {

                //单个送货的状态
                mmPaintLines.setColor(getResources().getColor(R.color.colorPrimaryDark));
                mmPaintLines.getTextBounds(info.text, 0, info.text.length(), rect);
                canvas.drawText(info.text, lineX - rect.width() / 2, awayY2, mmPaintLines);

//                //单个送货的状态    ======= 居中显示
//                mmPaintLines.setColor(getResources().getColor(R.color.send_state_gray));
//                mmPaintLines.getTextBounds(info.getText(), 0, info.getText().length(), rect);
//
//                float awayY = awayY2 / 2 + height / 2 + rect.height() / 2;//设置字体垂直居中计算高度
//                canvas.drawText(info.getText(), lineX - rect.width() / 2, awayY, mmPaintLines);

            }

        }


        //2 画最下层的文字///////////////end//////////////

        super.onDraw(canvas);
    }


    public void drawReal(int x, int y, Canvas canvas) {
//        绘制外圆
        this.mPaint.setARGB(255, 255, 108, 0);
        this.mPaint.setStrokeWidth(2);
        canvas.drawCircle(x, y, radius, this.mPaint);


        //绘制圆环
        this.mPaint.setColor(Color.WHITE);
//        this.mPaint.setStrokeWidth(ringWidth);
        canvas.drawCircle(x, y, radius - 3, this.mPaint);


        //绘制内圆
        this.mPaint.setARGB(255, 255, 108, 0);
        this.mPaint.setStrokeWidth(2);
        canvas.drawCircle(x, y, radius / 3, this.mPaint);
    }

    public List<Demo> mornal = new ArrayList<>();

    public void setMornal(List<Demo> mornal) {
        this.mornal = mornal;

        vivio = mornal.size();//控制显示多少个实心圆

        invalidate();
    }
}